﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using WebApplication5.Model;

namespace WebApplication5.Controller
{

    public class projectCtr
    {
        protected personCtr per = new personCtr();
        dmaa0912DannyEntities2 db = new dmaa0912DannyEntities2();

        public Project GetProject(int id)
        {
            var q = (from h in db.Project
                     where h.id == id
                     select h);

            return q.FirstOrDefault();
        }

        public void AddProject(Project p)
        {
            validationOfProject(p);

            db.Project.Add(p);
            db.SaveChanges();
        }

        public void editProject(Project ProjectUpdate)
        {
            validationOfProject(ProjectUpdate);

            Project project = db.Project.FirstOrDefault(h => h.id == ProjectUpdate.id);
            project.cost = ProjectUpdate.cost;
            project.endDate = ProjectUpdate.endDate;
            project.Login = ProjectUpdate.Login;
            project.loginId = ProjectUpdate.loginId;
            project.name = ProjectUpdate.name;
            project.startDate = ProjectUpdate.startDate;
            project.status = ProjectUpdate.status;
            project.time = ProjectUpdate.time;
            project.Userstory = ProjectUpdate.Userstory;

            db.SaveChanges();
        }

        public List<Project> GetAllProject()
        {
            try
            {
                var projects = from h in db.Project
                               select h;

                return projects.ToList();
            }
            catch (Exception e)
            {

            }
            return null;
        }



        public void deleteProject(int id)
        {
            Project p = (from v in db.Project
                         where v.id == id
                         select v).FirstOrDefault();

            db.Project.Remove(p);

            db.SaveChanges();
        }

        public List<Project> GetAllProjectId(int id)
        {
            try
            {
                var projects = from h in db.Project where id == h.loginId
                               select h;

                return projects.ToList();
            }
            catch (Exception e)
            {

            }
            return null;
        }

        internal List<Project> GetAllProjectIdsame(int id)
        {
            try
            {
                var projects = from h in db.Project
                               where id == h.id
                               select h;

                return projects.ToList();
            }
            catch (Exception e)
            {

            }
            return null;
        }


        private void validationOfProject(Project p)
        {

            Regex onlyDigits = new Regex("^[0-9]+$");
            Regex onlyLetters = new Regex(@"^[a-zA-Z]+$");
            Regex onlyLettersAndNumbers = new Regex(@"[a-zæøå 0-9]+");
            Regex onlyDates = new Regex(@"^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$@");

            if (!onlyDigits.IsMatch(p.cost.ToString()))
                throw new Exception("Kun tal tilladt i pris");

            if (string.IsNullOrEmpty(p.name))
                throw new Exception("Indtast et navn");

            if (string.IsNullOrEmpty(p.status))
                throw new Exception("Indtast en status");

            if (!onlyDigits.IsMatch(p.time.ToString()))
                throw new Exception("Kun tal tilladt i tid");

            if(!onlyDates.IsMatch(p.startDate.ToString()))
                throw new Exception("kun datoer tilladt");

            if(!onlyDates.IsMatch(p.endDate.ToString()))
                throw new Exception("kun datoer tilladt");


        }
    }
 }

